import {defineStore} from "pinia";
import {ref} from 'vue'
import {type SimpleUserInformationDto} from "../dto/SimpleUserInformationDto";
import {microServiceHttpServer} from "../apis/MicroServiceHttpServer";


export const useSimpleUserInformationStore = defineStore("simpleUserInformationStore", () => {
    const store = ref(new Map<Number, SimpleUserInformationDto>());
    async function get(id: number): Promise<SimpleUserInformationDto> {
        let dto;
        if (store.value.has(id)) {
            dto = store.value.get(id);
            if (dto != undefined) {
                return Promise.resolve(dto);
            }
        }
        let response = await microServiceHttpServer.getUserSimpleInformation(id)
        dto = JSON.parse(response.data);
        store.value.set(id, dto);
        return Promise.resolve(dto);
    }

    function set(id: number, newUserInformationDto: SimpleUserInformationDto) {
        store.value.set(id, newUserInformationDto);
    }

    return {store, get, set}
})